//
//  UIButton+BaseCreation.m
//  base
//
//  Created by Demi on 08/03/2017.
//  Copyright © 2017 Azazie. All rights reserved.
//



#import "UIButton+BaseCreation.h"
#import <objc/runtime.h>
#import "util.h"
#import "UIFont+Style.h"
#import "UIColor+Style.h"


#import <QMUIKit/QMUICore.h>

NSUInteger const PrimaryButtonBackgroundColorHex = 0xd68e96;
NSUInteger const PrimaryButtonDisabledBackgroundColorHex = 0xcccccc;
NSUInteger const PrimaryButtonHighlightedBackgroundColorHex = 0xce7e87;

NSUInteger const LPPrimaryButtonBackgroundColorHex = 0xE73A8F;   //0xEF6767;
NSUInteger const LPPrimaryButtonHighlightedBackgroundColorHex = 0xd73384;

@implementation UIButton (BaseCreation)

@dynamic hitTestEdgeInsets;

static const NSString *KEY_HIT_TEST_EDGE_INSETS = @"HitTestEdgeInsets";

-(void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets {
    NSValue *value = [NSValue value:&hitTestEdgeInsets withObjCType:@encode(UIEdgeInsets)];
    objc_setAssociatedObject(self, &KEY_HIT_TEST_EDGE_INSETS, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIEdgeInsets)hitTestEdgeInsets {
    NSValue *value = objc_getAssociatedObject(self, &KEY_HIT_TEST_EDGE_INSETS);
    if(value) {
        UIEdgeInsets edgeInsets; [value getValue:&edgeInsets]; return edgeInsets;
    }else {
        return UIEdgeInsetsZero;
    }
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if(UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero) || !self.enabled || self.hidden) {
        return [super pointInside:point withEvent:event];
    }
    
    CGRect relativeFrame = self.bounds;
    CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, self.hitTestEdgeInsets);
    
    return CGRectContainsPoint(hitFrame, point);
}

+ (UIButton *)primaryButton {
    
    UIButton *btn = [self buttonWithType:UIButtonTypeCustom];
    
    UIFont *font = nil;
    UIColor *normal = nil;
    UIColor *highlited = nil;
    UIColor *disabled = nil;
    
    if (IS_AZAZIE) {
        font = [UIFont dynamic_fontWithName:OrdinaryBold size:UIFontLargeSize];
        normal = [UIColor tx_colorWithHex:PrimaryButtonBackgroundColorHex];
        highlited = [UIColor tx_colorWithHex:PrimaryButtonHighlightedBackgroundColorHex];
        disabled = [UIColor tx_colorWithHex:PrimaryButtonDisabledBackgroundColorHex];
    }
    
    if (IS_LOVEPROM) {
        font = [UIFont dynamic_fontWithName:FunctionProDemi size:UIFontLargeSize];
        normal = [UIColor tx_colorWithHex:LPPrimaryButtonBackgroundColorHex];
        highlited = [UIColor tx_colorWithHex:LPPrimaryButtonHighlightedBackgroundColorHex];
        disabled = [UIColor tx_colorWithHex:PrimaryButtonDisabledBackgroundColorHex];
    }
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    [btn setBackgroundImage:[UIImage tx_imageWithColor:normal] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage tx_imageWithColor:highlited] forState:UIControlStateHighlighted];
    [btn setBackgroundImage:[UIImage tx_imageWithColor:disabled] forState:UIControlStateDisabled];
    return btn;
}

@end
